/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.TextView;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL;

import android.util.Log;
import android.util.FloatMath;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLDebugHelper;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ViewCore extends GLSurfaceView
{
	  //Instantiate rendering manager
	  RendererCore renderer;
	  private static final String TAG = ViewCore.class.getSimpleName();
	  ObjLoader loader;
	  float deltaX = 1.0f;
	  protected Timer timer;
	  Context ctext;
	  boolean isProcessingActionDown = false;
	  
	  public ViewCore(Context context)
	  {
		super(context);
		
		renderer = new RendererCore(context);
		
		ctext = context;
		timer = main.timer;
		setEGLConfigChooser(8, 8, 8, 8,0, 0);
		
	    getHolder().setFormat(PixelFormat.RGBA_8888);
	   
	   
	   

	  
	    
		setRenderer(renderer);
		//setRenderMode(RENDERMODE_WHEN_DIRTY);

	}
	  public void setTimer(Timer worldTimer)
	  {
		  timer = worldTimer;
		  renderer.setTimer(timer);
	  }
	  
	  public boolean onTouchEvent(final MotionEvent event)
	  {
		  if(event.getEventTime() > 500 && !isProcessingActionDown )
		  {
		  
		   if(event.getAction()==event.ACTION_DOWN )

	        {
			   isProcessingActionDown = true;
			  
	            try {
	            	 if(event.getPointerCount() <2)
	                queueEvent(new Runnable() {

	                    public void run() {

	                    //    renderer.debugevent(event.getAction());

	                    renderer.onTouchEvent(event);

	                    }

	                });

	              Thread.sleep(33);
	              isProcessingActionDown = false;
	            } catch (InterruptedException e) 
	            {

	                // TODO Auto-generated catch block

	                e.printStackTrace();

	            }

	        }}
		  
		  return true;
	}
	 
	 
	public void onPause()
    {
		renderer.reloadTextures();
    }
	

	  
	  
	 
	
	
}
